package adger.me.yili.view.impl;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;

import net.soulwolf.widget.speedyselector.widget.SelectorTextView;

import org.xutils.DbManager;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

import adger.me.yili.R;
import adger.me.yili.activity.OrderDetailMActivity;
import adger.me.yili.bean.OrdersBeanTable;
import adger.me.yili.utils.Arith;
import adger.me.yili.utils.DB;
import adger.me.yili.utils.StrUtil;
import adger.me.yili.view.CommonAdapter;
import adger.me.yili.view.ViewHolder;
import butterknife.Bind;
import butterknife.ButterKnife;

public class AllOrderFragment extends Fragment {
    @Bind(R.id.lv_orders)
    ListView lv_orders;
    @Bind(R.id.stv_total)
    SelectorTextView stv_total;
    private String mTitle;
    private CommonAdapter<OrdersBeanTable.OrdersEntity> mAdapter;
    private List<OrdersBeanTable.OrdersEntity> mDatas = new ArrayList<OrdersBeanTable.OrdersEntity>();
    WhereBuilder mwhereBuilder;

    public static AllOrderFragment getInstance(String title, WhereBuilder mwhereBuilder) {
        AllOrderFragment sf = new AllOrderFragment();
        sf.mTitle = title;
        sf.mwhereBuilder = mwhereBuilder;
        return sf;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_all_order, null);
        ButterKnife.bind(this, view);
        setAdapter();
        initData();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
//        Logger.d("onresume");
        initData();
//        DbManager dbManager = DB.getXInstance();
//        try {
//            //测试
//            OrdersBeanTable.OrdersEntity entity=   dbManager.selector(OrdersBeanTable.OrdersEntity.class).where("user_sn", "=", "0429").findFirst();
//            Logger.d(" entity onResume after ------Confirmed->"+ entity.getConfirmed()+"    Claimed"+entity.getClaimed());
//        } catch (DbException e) {
//            e.printStackTrace();
//        }
        mAdapter.notifyDataSetChanged();
    }

    private void setAdapter() {

        //mdatas 记得加数据进去
        mAdapter = new CommonAdapter<OrdersBeanTable.OrdersEntity>(getActivity(), mDatas, R.layout.item) {

            @Override
            public void convert(ViewHolder helper, int position, OrdersBeanTable.OrdersEntity itemEntity) {
//                Logger.d(item.getUser_sn()+"    "+item.getCharge());
                OrdersBeanTable.OrdersEntity item = mDatas.get(position);
                if (item.getDeliver() == 1) {//换了背景色 有复用  其他item需要重置为之前的背景色 类似checkboxs
                    helper.setSETBgAndTextColor(R.id.count_item, Color.parseColor("#FF4081"), Color.parseColor("#FFFFFF"));
                } else {
                    helper.setSETBgAndTextColor(R.id.count_item, Color.parseColor("#84b067"), Color.parseColor("#FFFFFF"));
                }

                if (item.getConfirmed() == 1 && item.getClaimed() == 0) {
                    helper.setSETBgAndTextColor(R.id.username_item, Color.parseColor("#FF4081"), Color.parseColor("#FFFFFF"));
                } else {
                    helper.setSETBgAndTextColor(R.id.username_item, Color.parseColor("#84b067"), Color.parseColor("#FFFFFF"));

                }
                helper.setText(R.id.count_item, position + 1 + "");
                helper.setText(R.id.username_item, item.getUser());

                if (String.valueOf(item.getCharge()).equals("0.0") || String.valueOf(item.getCharge()).equals("0")) {
                    helper.setText(R.id.money_item, "");
                } else {
                    helper.setText(R.id.money_item, String.valueOf(item.getCharge()));
                }

                helper.setOnclickListener(R.id.find_item,new MClickListener(position));
            }

        };
        lv_orders.setAdapter(mAdapter);
    }

    private  class MClickListener implements View.OnClickListener {
        int position;
        MClickListener(int position) {
            this.position=position;
        }
        @Override
        public void onClick(View v) {
            OrdersBeanTable.OrdersEntity item = mDatas.get(position);
            String ordersEntityStr = new Gson().toJson(item);
            Intent intent = new Intent(getActivity(), OrderDetailMActivity.class);
            intent.putExtra("ordersEntityStr", ordersEntityStr);
            startActivity(intent);
        }
    }

    private void initData() {
        DbManager dbManager = DB.getXInstance();
        try {
//            List<OrdersBeanTable.OrdersEntity> ordersEntityList = dbManager.selector(OrdersBeanTable.OrdersEntity.class).findAll();
//            List<OrdersBeanTable.OrdersEntity> ordersEntityList = dbManager.selector(OrdersBeanTable.OrdersEntity.class).where(WhereBuilder.b("confirmed", "=", 1).and("claimed", " = ", 1)).findAll();
            List<OrdersBeanTable.OrdersEntity> ordersEntityList = dbManager.selector(OrdersBeanTable.OrdersEntity.class).where(mwhereBuilder).findAll();
            mDatas.clear();
            mAdapter.notifyDataSetChanged();
            if (ordersEntityList == null || ordersEntityList.size() == 0) {
                return;
            }
            mDatas.addAll(ordersEntityList);
            mAdapter.notifyDataSetChanged();
            float temp = 0;
            for (OrdersBeanTable.OrdersEntity item : ordersEntityList) {
                temp= Arith.add(temp, item.getCharge());
            }
            float tempFloat = Float.parseFloat(StrUtil.formateRate(temp + ""));

            stv_total.setText(tempFloat + "");
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}